package com.china08.yxyapi.api.schwebsite.article;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.ThumborUtil;

/***
 * 学校网站:列表页-全部
 * 
 * @author Ertao.Fang
 *
 */
@CrossOrigin
@RestController("SwsArticleListForSection1Controller")
public class SwsArticleListForSection1Controller extends YxyApiBaseController {

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private SwsArticleRepository swsArticleRepository;

    @Autowired
    private SwsSectionRepository swsSectionRepository;

    @RequestMapping(value = "/api_yw/schwebsite/article/section1", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);
        String section1Id = reqModel.section1Id;
        String section2Id = reqModel.section2Id;
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        Assert.isTrue(StringUtils.isNotBlank(section1Id), "栏目ID为空");

        // 通过域名获取学校ID
        SwsSettingDomains settingDomains = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != settingDomains, "域名对象为空");
        String schoolId = settingDomains.getSchoolId();

        Pageable pageable = reqModel.getPageable(Direction.DESC, "isSticky", "showDate");
        RespModel model = new RespModel();
        Integer auditStatus = 1;
        Page<SwsArticle> swsArticle = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndAuditStatus(schoolId,
                section1Id, auditStatus, pageable);
        Assert.isTrue(null != swsArticle, "文章列表为空");
        List<Article> list = new ArrayList<Article>();
        SectionName sectionName = new SectionName();
        if (StringUtils.isNotBlank(section1Id) && StringUtils.isBlank(section2Id)) {
            SwsSection swsSection = swsSectionRepository.findById(section1Id);
            sectionName.sectionName1 = swsSection.getSectionName();

        } else if (StringUtils.isNotBlank(section2Id) && StringUtils.isBlank(section1Id)) {
            SwsSection swsSection = swsSectionRepository.findById(section2Id);
            sectionName.sectionName2 = swsSection.getSectionName();
        } else if (StringUtils.isNotBlank(section2Id) && StringUtils.isNotBlank(section1Id)) {
            SwsSection swsSection = swsSectionRepository.findById(section1Id);
            sectionName.sectionName1 = swsSection.getSectionName();
            SwsSection swsSection2 = swsSectionRepository.findById(section2Id);
            sectionName.sectionName2 = swsSection2.getSectionName();
        } else {
            sectionName.sectionName2 = "栏目为空";
        }
        model.sectionName = sectionName;
        String displayStyle = null;
        SwsSection swsSection = swsSectionRepository.findBySchoolIdAndId(schoolId, section1Id);
        for (SwsArticle swsArticle2 : swsArticle) {
            displayStyle = swsSection.getDisplayStyle().toString();
            Article article = new Article();
            if (StringUtils.isNotBlank(swsArticle2.getFaceImg())) {
                article.faceImg = ThumborUtil.schoolWebSiteResize(swsArticle2.getFaceImg(), 180, 124);
            } else {
                article.faceImg = "";
            }

            if (StringUtils.isBlank(article.faceImg)) {
                // 获取详情中的第一张图片作为推荐图片
                Document document = Jsoup.parse(swsArticle2.getContent());
                Elements elements = document.select("img");
                if (elements.size() > 0) {
                    String attr = elements.get(0).attr("src");
                    article.faceImg = ThumborUtil.schoolWebSiteResize(attr, 180, 124);
                }

            }
            if (StringUtils.isNotBlank(swsArticle2.getFaceImg())) {
                article.faceImg_140_88 = ThumborUtil.schoolWebSiteResize(swsArticle2.getFaceImg(), 140, 88);
            } else {
                article.faceImg_140_88 = "";
            }

            article.id = swsArticle2.getId();
            if (displayStyle.equals("IMG_LIST")) {
                article.showDate = DateToolUtils.format(swsArticle2.getShowDate(), DateToolUtils.FORMAT_LONG);
                article.showDate1 = DateToolUtils.format(swsArticle2.getShowDate(), DateToolUtils.FORMAT_SHORT_YMD);
            } else {
                article.showDate = DateToolUtils.format(swsArticle2.getShowDate(), DateToolUtils.FORMAT_SHORT_YMD);

            }
            article.author = swsArticle2.getAuthor();
            article.showDateApp = DateToolUtils.format(swsArticle2.getShowDate(), DateToolUtils.FORMAT_SHORT_YMD);
            article.summary = swsArticle2.getSummary();
            article.title = swsArticle2.getTitle();
            String html = swsArticle2.getContent();
            article.content = Jsoup.parse(html).text();
            if (StringUtils.isBlank(swsArticle2.getSection2Id())) {
                article.section2Id = "0";
            } else {
                article.section2Id = swsArticle2.getSection2Id();
            }
            list.add(article);
        }
        model.displayStyle = displayStyle;
        model.ArticleList = new PageImpl<Article>(list, pageable, swsArticle.getTotalElements());

        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String section1Id;
        public String section2Id;

    }

    protected static class SectionName {

        public String sectionName1;

        public String sectionName2;
    }

    protected static class Article {

        public String id;

        public String faceImg;

        public String faceImg_140_88;

        public String title;

        public String summary;

        public String showDate;

        public String showDate1;

        public String showDateApp;

        public String section2Id;

        public String content;

        public String author;

    }

    protected static class RespModel extends YxyApiRespModel {

        public SectionName sectionName;

        public String displayStyle;

        public Page<Article> ArticleList;

    }
}
